package com.health.commons;

import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

 

/**
 * AES Coder<br/>
 * secret key length:	128bit, default:	128 bit<br/>
 * mode:	ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128<br/>
 * padding:	Nopadding/PKCS5Padding/ISO10126Padding/
 * @author Aub
 * 
 */
public class AESCoder {
	
	public static final String CBC_NoPadding = "AES/CBC/NoPadding";
	public static final String CBC_PKCS5Padding = "AES/CBC/PKCS5Padding";
	public static final String CBC_ISO10126Padding = "AES/CBC/ISO10126Padding";
	public static final String CFB_NoPadding = "AES/CFB/NoPadding";
	public static final String CFB_PKCS5Padding = "AES/CFB/PKCS5Padding";
	public static final String CFB_ISO10126Padding = "AES/CFB/ISO10126Padding";
	public static final String ECB_NoPadding = "AES/ECB/NoPadding";
	public static final String ECB_PKCS5Padding = "AES/ECB/PKCS5Padding";
	public static final String ECB_ISO10126Padding = "AES/ECB/ISO10126Padding";
	public static final String OFB_NoPadding = "AES/OFB/NoPadding";
	public static final String OFB_PKCS5Padding = "AES/OFB/PKCS5Padding";
	public static final String OFB_ISO10126Padding = "AES/OFB/ISO10126Padding";
	public static final String PCBC_NoPadding = "AES/PCBC/NoPadding";
	public static final String PCBC_PKCS5Padding = "AES/PCBC/PKCS5Padding";
	public static final String PCBC_ISO10126Padding = "AES/PCBC/ISO10126Padding";
	
	/**
	 * 密钥算法
	*/
	private static final String KEY_ALGORITHM = "AES";
	
	private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
	
	/**
	 * 初始化密�?
	 * 
	 * @return byte[] 密钥 
	 * @throws Exception
	 */
	public static byte[] initSecretKey() {
		//返回生成指定算法的秘密密钥的 KeyGenerator 对象
		KeyGenerator kg = null;
		try {
			kg = KeyGenerator.getInstance(KEY_ALGORITHM);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return new byte[0];
		}
		//初始化此密钥生成器，使其具有确定的密钥大�?
		//AES 要求密钥长度�?128
		kg.init(128);
		//生成�?��密钥
		SecretKey  secretKey = kg.generateKey();
		return secretKey.getEncoded();
	}
	
	/**
	 * 转换密钥
	 * 
	 * @param key	二进制密�?
	 * @return 密钥
	 */
	private static Key toKey(byte[] key){
		//生成密钥
		return new SecretKeySpec(key, KEY_ALGORITHM);
	}
	
	/**
	 * 加密
	 * 
	 * @param data	待加密数�?
	 * @param key	密钥
	 * @return byte[]	加密数据
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] data,Key key) throws Exception{
		return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
	}
	
	/**
	 * 加密
	 * 
	 * @param data	待加密数�?
	 * @param key	二进制密�?
	 * @return byte[]	加密数据
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] data,byte[] key) throws Exception{
		return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
	}
	
	
	/**
	 * 加密
	 * 
	 * @param data	待加密数�?
	 * @param key	二进制密�?
	 * @param cipherAlgorithm	加密算法/工作模式/填充方式
	 * @return byte[]	加密数据
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{
		//还原密钥
		Key k = toKey(key);
		return encrypt(data, k, cipherAlgorithm);
	}
	
	/**
	 * 加密
	 * 
	 * @param data	待加密数�?
	 * @param key	密钥
	 * @param cipherAlgorithm	加密算法/工作模式/填充方式
	 * @return byte[]	加密数据
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{
		//实例�?
		Cipher cipher = Cipher.getInstance(cipherAlgorithm);
		//使用密钥初始化，设置为加密模�?
		cipher.init(Cipher.ENCRYPT_MODE, key);
		//执行操作
		return cipher.doFinal(data);
	}
	
	
	
	/**
	 * 解密
	 * 
	 * @param data	待解密数�?
	 * @param key	二进制密�?
	 * @return byte[]	解密数据
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] data,byte[] key) throws Exception{
		return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
	}
	
	/**
	 * 解密
	 * 
	 * @param data	待解密数�?
	 * @param key	密钥
	 * @return byte[]	解密数据
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] data,Key key) throws Exception{
		return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
	}
	
	/**
	 * 解密
	 * 
	 * @param data	待解密数�?
	 * @param key	二进制密�?
	 * @param cipherAlgorithm	加密算法/工作模式/填充方式
	 * @return byte[]	解密数据
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{
		//还原密钥
		Key k = toKey(key);
		return decrypt(data, k, cipherAlgorithm);
	}

	/**
	 * 解密
	 * 
	 * @param data	待解密数�?
	 * @param key	密钥
	 * @param cipherAlgorithm	加密算法/工作模式/填充方式
	 * @return byte[]	解密数据
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{
		//实例�?
		Cipher cipher = Cipher.getInstance(cipherAlgorithm);
		//使用密钥初始化，设置为解密模�?
		cipher.init(Cipher.DECRYPT_MODE, key);
		//执行操作
		return cipher.doFinal(data);
	}
	
	private static String  showByteArray(byte[] data){
		if(null == data){
			return null;
		}
		StringBuilder sb = new StringBuilder("{");
		for(byte b:data){
			sb.append(b).append(",");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append("}");
		return sb.toString();
	}
	
	public static void main(String[] args) throws Exception {
		byte[] key =  "0123456789abcdef".getBytes();
		
		String data ="2013-12-31 23:59:59";
		
		String[] list = {"AES/CBC/NoPadding",
				"AES/CBC/PKCS5Padding",
				"AES/CBC/ISO10126Padding",
				"AES/CFB/NoPadding",
				"AES/CFB/PKCS5Padding",
				"AES/CFB/ISO10126Padding",
				"AES/ECB/NoPadding",
				"AES/ECB/PKCS5Padding",
				"AES/ECB/ISO10126Padding",
				"AES/OFB/NoPadding",
				"AES/OFB/PKCS5Padding",
				"AES/OFB/ISO10126Padding",
				"AES/PCBC/NoPadding",
				"AES/PCBC/PKCS5Padding",
				"AES/PCBC/ISO10126Padding"};
		
		for (String cipherAlgorithm : list) {
			try {
				

//			System.out.println("加密前数�? string:"+data);
//			System.out.println("加密前数�? byte[]:"+showByteArray(data.getBytes()));
			System.out.println(cipherAlgorithm);
			byte[] encryptData = encrypt(data.getBytes(), key, cipherAlgorithm);
//			System.out.println("加密后数�? byte[]:"+showByteArray(encryptData));
			System.out.println("加密后数�? hexStr:"+StrConvertUtil.byteArrToHexStr(encryptData));
//			System.out.println();
//			byte[] decryptData = decrypt(encryptData, key, cipherAlgorithm);
//			System.out.println("解密后数�? byte[]:"+showByteArray(decryptData));
//			System.out.println("解密后数�? string:"+new String(decryptData));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
				
	}
}